import time  
import paho.mqtt.client as mqtt
import json
from util import send_data_to_mqtt
from setting import *
from sentry import TaskSentry
  
def main():  
    with open('/proc/gpio/pwrdet','r') as pwrdet_file:  

        pwrdet = pwrdet_file.read().strip()

        print(pwrdet)  

        if pwrdet == '1':

            data = {
                
                "type": "StationPowerDown",
                "data":{
                        "stationType": "MasterStation", # masterStation
                        "stationAddr": MAC
                }									
                
            }

            TaskSentry.mqtt_send_data_to_data_center(json.dumps(data))

            time.sleep(60)

            with open('/proc/gpio/pwren','w') as pwren_file:

                pwrdet_file.write('0')
  
if __name__ == "__main__": 

    while True:

        main()
        
        time.sleep(10)
